using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.TextEditor.Document;
using RideMe.Presentation.Properties;
using RideMe.Core.Parser;
using RideMe.Core.SortableCollection;

namespace RideMe.Presentation
{
    public partial class ClassMethodDropDowns : UserControl
    {
        public ClassMethodDropDowns()
        {
            InitializeComponent();
        }

        private const int Pad = 2;

        public event EventHandler SelectedTopLevelEntityChanged;
        public event EventHandler SelectedMethodChanged;

        protected virtual void OnSelectedTopLevelEntityChanged(EventArgs e)
        {
            if (SelectedTopLevelEntityChanged != null)
            {
                SelectedTopLevelEntityChanged(this, e);
            }
        }

        protected virtual void OnSelectedMethodChanged(EventArgs e)
        {
            if (SelectedMethodChanged != null)
            {
                SelectedMethodChanged(this, e);
            }
        }
        
        public TopLevelEntity SelectedTopLevelEntity
        {
            get 
            {
                if (classesComboBox.Items.Count > 0)
                {
                    return classesComboBox.SelectedItem as TopLevelEntity; 
                }
                return null;
                
            }
        }

        public Method SelectedMethod
        {
            get
            {
                if (methodsComboBox.Items.Count > 0)
                {
                    return methodsComboBox.SelectedItem as Method;
                }
                return null;
            }
        }

        private void PositionAndSizeControls()
        {
            classesComboBox.Width = (this.Width - (Pad * Pad) - Pad) / 2;
            classesComboBox.Left = Pad;
            methodsComboBox.Width = classesComboBox.Width;
            methodsComboBox.Left = classesComboBox.Width + Pad * Pad;
        }

        public void LoadDropDowns(SortableCollection<TopLevelEntity> topLevelEntities)
        {
            classesComboBox.Items.Clear();

            foreach (TopLevelEntity topLevelEntity in topLevelEntities)
            {
                classesComboBox.Items.Add(topLevelEntity);
                AddSubEntities(topLevelEntity);
            }

            if (classesComboBox.Items.Count > 0)
            {
                classesComboBox.SelectedIndex = 0;
            }

            methodsComboBox.Items.Clear();

            AddMethods(classesComboBox.SelectedItem as TopLevelEntity);

            if (methodsComboBox.Items.Count > 0)
            {
                methodsComboBox.SelectedIndex = 0;
            }

        }

        private void AddSubEntities(TopLevelEntity topLevelEntity)
        {
            // This makes for a much nicer display as the user types.

            classesComboBox.SuspendLayout();

            foreach (TopLevelEntity entity in topLevelEntity.SubEntities)
            {
                // Set the name for display, Yo. TODO: Move this to the parser.
                entity.Name = string.Format("{0}{1}{2}", topLevelEntity.Name, 
                    Resources.RubyClassSeparator, entity);
                classesComboBox.Items.Add(entity);
                AddSubEntities(entity);
            }

            classesComboBox.ResumeLayout();
        }

        private void AddMethods(TopLevelEntity topLeveEntity)
        {
            if (topLeveEntity == null) return;

            if (topLeveEntity.Methods.Count > 0)
            {
                // This makes for a much nicer refresh. 
                methodsComboBox.SuspendLayout();
               
                topLeveEntity.Methods.Sort(Resources.IRubyEntitySortColumn, SortDirection.Asc);

                foreach (Method method in topLeveEntity.Methods)
                {
                    methodsComboBox.Items.Add(method);
                }
                methodsComboBox.ResumeLayout();
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            PositionAndSizeControls();
        }

        private void classesComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            methodsComboBox.Items.Clear();

            AddMethods(classesComboBox.SelectedItem as TopLevelEntity);

            if (methodsComboBox.Items.Count > 0)
            {
                methodsComboBox.SelectedIndex = 0;
            }

            OnSelectedTopLevelEntityChanged(EventArgs.Empty);

        }

        private void methodsComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            OnSelectedMethodChanged(EventArgs.Empty);
        }

    }
}
